home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.5)
-
- import sys
- import warnings
- import xml
- import urllib2
- import codecs
- import locale
- import StringIO
- import zipfile
- import gzip
- from htmlentitydefs import name2codepoint, codepoint2name
- from collections import deque
- from codecs import CodecInfo
-
- try:
- import pylzma
- HAVE_LZMA = True
- except ImportError:
- HAVE_LZMA = False
-
- ENCODE_LZMA = False
- __simplechars_enc = {
- ord('<'): 'lt',
- ord('>'): 'gt',
- ord('"'): 'quot',
- ord('&'): 'amp' }
- __simplechars_dec = dict((lambda .0: for k, v in .0:
- (v, unichr(k)))(__simplechars_enc.items()))
- __simplechars_dec['apos'] = unichr(ord("'"))
- _encodings = [
- locale.getpreferredencoding(),
- 'ISO-8859-1',
- sys.getfilesystemencoding(),
- sys.getdefaultencoding()]
-
- def register_codec(name, encode, decode):
-
- def _search(n):
- if n == name:
- return CodecInfo(name = name, encode = encode, decode = decode)
-
-
- codecs.register(_search)
-
-
- def fuzzydecode(s, encoding = None, errors = 'strict'):
- if isinstance(s, unicode):
- warnings.warn('decoding unicode is not supported!')
- return s
-
- encodings = list(_encodings)
- if isinstance(encoding, basestring):
- encodings.insert(0, encoding)
- elif encoding is None:
- pass
- else:
- encodings = list(encoding) + encodings
- for e in encodings:
-
- try:
- return s.decode(e, errors)
- continue
- except (UnicodeDecodeError, LookupError):
- e = None
- continue
-
-
-
- return s.decode(encoding, 'replace')
-
-
- def fuzzyencode(s, errors = 'strict'):
- raise NotImplementedError
-
-
- def _xml_encode(input, errors = 'simple'):
- simple = 'simple' in errors
- origtype = type(input)
- if simple:
- chars = __simplechars_enc
- else:
- chars = codepoint2name
- res = []
- append = res.append
- for ch in input:
- och = ord(ch)
- if och in chars:
- append('&%s;' % chars[och])
- continue
- append(ch)
-
- return (origtype(''.join(res)), len(input))
-
-
- def _xml_decode(input, errors = 'strict'):
- data = deque(input)
- res = []
- append = res.append
- popleft = data.popleft
- extendleft = data.extendleft
- while data:
- ch = popleft()
- if ch == '&':
- curtoken = ''
- is_ref = False
- is_num = False
- is_hex = False
- while len(curtoken) < 10 and data:
- nch = popleft()
- if nch == '#':
- is_num = True
-
- if is_num and len(curtoken) == 1 and nch == 'x':
- is_hex = True
-
- if nch == ';':
- is_ref = True
- break
-
- curtoken += nch
- if not is_ref:
- extendleft(reversed(curtoken))
- append('&')
- continue
- elif is_num:
- curtoken = curtoken[1:]
- if is_hex:
- curtoken = curtoken[1:]
- och = int(curtoken, 16)
- else:
- och = int(curtoken, 10)
- append(unichr(och))
- elif curtoken in name2codepoint:
- append(unichr(name2codepoint[curtoken]))
- elif curtoken in __simplechars_dec:
- append(__simplechars_dec[curtoken])
- else:
- append('&%s;' % curtoken)
- is_ref
- append(ch)
- return (u''.join(res), len(input))
-
- register_codec('xml', _xml_encode, _xml_decode)
-
- def _pk_decode(input, errors = 'strict'):
- li = len(input)
- input = StringIO.StringIO(input)
- z = zipfile.ZipFile(input, mode = 'rb')
- zi = z.filelist[0]
- return (z.read(zi.filename), li)
-
-
- def _pk_encode(input, errors = 'strict'):
- li = len(input)
- s = StringIO.StringIO()
- z = zipfile.ZipFile(s, mode = 'wb', compression = zipfile.ZIP_DEFLATED)
- z.writestr('file', input)
- z.close()
- return (s.getvalue(), li)
-
-
- def _gzip_decode(input, errors = 'strict'):
- li = len(input)
- input = StringIO.StringIO(input)
- g = gzip.GzipFile(mode = 'rb', fileobj = input)
- return (g.read(), li)
-
-
- def _gzip_encode(input, errors = 'strict'):
- li = len(input)
- s = StringIO.StringIO()
- g = gzip.GzipFile(mode = 'wb', fileobj = s)
- g.write(input)
- g.close()
- return (s.getvalue(), li)
-
-
- def _fuzzyzip_decode(input, errors = 'strict'):
- magic_num = input[:2]
- if magic_num == 'PK':
- return _pk_decode(input, errors = 'strict')
- elif magic_num == 'BZ':
- return (input.decode('bz2'), len(input))
- elif magic_num == '\x1f\x8b':
- return _gzip_decode(input, errors = 'strict')
- elif HAVE_LZMA:
-
- try:
- return (pylzma.decompress(input), len(input))
- except Exception:
- pass
- except:
- None<EXCEPTION MATCH>Exception
-
-
- None<EXCEPTION MATCH>Exception
- return (input.decode('zip'), len(input))
-
-
- def _fuzzyzip_encode(input, errors = 'strict'):
- li = len(input)
- funcs = [
- ((lambda : input.encode('bz2')),),
- (lambda : input.encode('zlib'))]
- if ENCODE_LZMA and HAVE_LZMA:
- (funcs.append,)((lambda : pylzma.compress(input)))
-
- shortest_val = None
- shortest_len = -1
- for func in funcs:
- newval = func()
- newlen = len(newval)
- if shortest_len < 0 or newlen < shortest_len:
- shortest_len = newlen
- shortest_val = newval
- continue
-
- return (shortest_val, li)
-
-
- def search(name):
- if name == 'z' or name == 'fuzzyzip':
- name = 'fuzzyzip'
- encode = _fuzzyzip_encode
- decode = _fuzzyzip_decode
- return CodecInfo(name = 'fuzzyzip', encode = _fuzzyzip_encode, decode = _fuzzyzip_decode)
-
-
- codecs.register(search)
- del search
-
- def search(name):
- if name.startswith('fuzzy'):
- if name == 'fuzzyzip':
- return None
-
- if not name[len('fuzzy'):]:
- pass
- encoding = None
- elif name.endswith('?'):
- if not name[:-1]:
- pass
- encoding = None
- else:
- return None
- name = 'fuzzy'
- encode = fuzzyencode
-
- def decode(s, errors = ('strict',)):
- return (fuzzydecode(s, encoding, errors), len(s))
-
- return CodecInfo(name = name, encode = encode, decode = decode)
-
- codecs.register(search)
- del search
- __locale_encoding = locale.getpreferredencoding()
- register_codec('locale', (lambda s: (s.encode(__locale_encoding), len(s))), (lambda s: (s.decode(__locale_encoding), len(s))))
- __filesysencoding = sys.getfilesystemencoding()
-
- def _filesys_encode(s):
- if isinstance(s, str):
- return (s, len(s))
- else:
- return (s.encode(__filesysencoding), len(s))
-
-
- def _filesys_decode(s):
- if isinstance(s, unicode):
- return (s, len(s))
- else:
- return (s.decode(__filesysencoding), len(s))
-
- register_codec('filesys', _filesys_encode, _filesys_decode)
- del _filesys_encode
- del _filesys_decode
-
- def _url_encode(input, errors = 'strict'):
- return (urllib2.quote(input), len(input))
-
-
- def _url_decode(input, errors = 'strict'):
- return (urllib2.unquote(input), len(input))
-
- register_codec('url', _url_encode, _url_decode)
- __all__ = []
- if __name__ == '__main__':
-
- def gen_rand_str(length = 5000):
- randint = randint
- randchoice = choice
- import random
- ascii_letters = ascii_letters
- import string
- ents = list(__simplechars_dec)
- data = []
- append = data.append
- for x in xrange(length):
- r = randint(0, 10)
- if r == 0:
- append('&%s;' % randchoice(ents))
- elif r == 1:
- append('&%d;' % randint(0, 65535))
- elif r == 2:
- append('&%x;' % randint(0, 65535))
-
- if r > 3:
- append(randchoice(ascii_letters))
- continue
-
- return ''.join(data)
-
- strings = [ gen_rand_str() for x in xrange(100) ]
- results1 = []
- results2 = []
- from time import clock
-
- def timeit(func):
- before = clock()
- func()
- return clock() - before
-
-
- def foo(encoding, res):
- for s in strings:
- res.append(s.decode(encoding))
-
-
- print 'xml', timeit((lambda : foo('xml', results1)))
- print 'xml2', timeit((lambda : foo('xml2', results2)))
-
-